package com.app.micaihu.ad.custom;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.AdConfig;
import com.app.micaihu.bean.ad.NativeAd;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoHeightControllerListener;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.user.userinfo.activity.WebActivity;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;

import java.util.HashMap;

/**
 * Created by linkai on 2016/10/20.
 */
public class H5NativeView extends RelativeLayout implements View.OnClickListener {


    private NativeAd nativeAd;//显示的广告

    /************ 控件 *************/
    private CustomImageView smallImg;
    private CustomImageView bigImg;
    private CustomImageView oneImg;
    private CustomImageView twoImg;
    private CustomImageView threeImg;
    private TextView title;
    private TextView desc;
    private TextView desc1;
    private TextView desc2;
    private ImageView mark;
    private ImageView mark1;
    private ImageView mark2;

    private int width = 300;
    private int height = 215;
    private AutoHeightControllerListener autoHeightControllerListener;

    private HashMap<String, Object> mapEvent;

    public H5NativeView(Context context) {
        super(context);
        initView();
    }

    public H5NativeView(Context context, HashMap<String, Object> mapEvent) {
        super(context);
        this.mapEvent = mapEvent;
        initView();
    }

    /**
     * 初始化公共布局
     */
    protected void initView() {

        LayoutInflater.from(getContext()).inflate(R.layout.ad_h5, this);
        this.title = (TextView) findViewById(R.id.item_public_title);
        this.desc = (TextView) findViewById(R.id.ad_desc);
        this.desc1 = (TextView) findViewById(R.id.ad_desc1);
        this.desc2 = (TextView) findViewById(R.id.ad_desc2);
        this.mark = (ImageView) findViewById(R.id.ad_mark);
        this.mark1 = (ImageView) findViewById(R.id.ad_mark1);
        this.mark2 = (ImageView) findViewById(R.id.ad_mark2);
        this.oneImg = (CustomImageView) findViewById(R.id.item_image_0);
        this.twoImg = (CustomImageView) findViewById(R.id.item_image_1);
        this.threeImg = (CustomImageView) findViewById(R.id.item_image_2);
        this.smallImg = (CustomImageView) findViewById(R.id.item_small_image);
        this.bigImg = (CustomImageView) findViewById(R.id.item_large_image);

        ViewSizeUtils.changeViewAuto1080(smallImg, width, height);
        ViewSizeUtils.changeViewAuto1080(oneImg, width, height);
        ViewSizeUtils.changeViewAuto1080(twoImg, width, height);
        ViewSizeUtils.changeViewAuto1080(threeImg, width, height);

        setOnClickListener(this);
        autoHeightControllerListener = new AutoHeightControllerListener(this.bigImg, 4.35f, 1.58f);
    }

    /**
     * 设置即将加载的广告
     *
     * @param nativeAd
     */
    public void setNativeAd(NativeAd nativeAd) {
        if (nativeAd == null) {
            return;
        }
        this.title.setText(nativeAd.getTitle());
        if (TextUtils.equals(nativeAd.getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_SMALL)) {

            if (nativeAd.getThumbList() != null) {
                if (nativeAd.getThumbList().length >= 1
                        && nativeAd.getThumbList().length <= 2) {
                    LoadImageUtils.getInstance().loadImage(this.smallImg, nativeAd.getThumbList()[0]);
                    this.oneImg.setVisibility(View.GONE);
                    this.twoImg.setVisibility(View.GONE);
                    this.threeImg.setVisibility(View.GONE);
                    this.smallImg.setVisibility(View.VISIBLE);
                    this.bigImg.setVisibility(View.GONE);
                    //显示一张图的描述
                    this.desc.setText(nativeAd.getSummary());
                    this.desc.setVisibility(View.VISIBLE);
                    this.desc1.setVisibility(View.GONE);
                    this.desc2.setVisibility(View.GONE);
                    this.mark.setVisibility(View.VISIBLE);
                    this.mark1.setVisibility(View.GONE);
                    this.mark2.setVisibility(View.GONE);
                } else if (nativeAd.getThumbList().length >= 3) {
                    LoadImageUtils.getInstance().loadImage(this.oneImg, nativeAd.getThumbList()[0]);
                    LoadImageUtils.getInstance().loadImage(this.twoImg, nativeAd.getThumbList()[1]);
                    LoadImageUtils.getInstance().loadImage(this.threeImg, nativeAd.getThumbList()[2]);
                    this.oneImg.setVisibility(View.VISIBLE);
                    this.twoImg.setVisibility(View.VISIBLE);
                    this.threeImg.setVisibility(View.VISIBLE);
                    this.smallImg.setVisibility(View.GONE);
                    this.bigImg.setVisibility(View.GONE);
                    //显示三张图的描述
                    this.desc1.setText(nativeAd.getSummary());
                    this.desc.setVisibility(View.GONE);
                    this.desc1.setVisibility(View.VISIBLE);
                    this.desc2.setVisibility(View.GONE);
                    this.mark.setVisibility(View.GONE);
                    this.mark1.setVisibility(View.VISIBLE);
                    this.mark2.setVisibility(View.GONE);
                } else {
                    this.oneImg.setVisibility(View.GONE);
                    this.twoImg.setVisibility(View.GONE);
                    this.threeImg.setVisibility(View.GONE);
                    this.smallImg.setVisibility(View.GONE);
                    this.bigImg.setVisibility(View.GONE);
                    this.desc.setText(nativeAd.getSummary());
                    this.desc.setVisibility(View.VISIBLE);
                    this.desc1.setVisibility(View.GONE);
                    this.desc2.setVisibility(View.GONE);
                    this.mark.setVisibility(View.VISIBLE);
                    this.mark1.setVisibility(View.GONE);
                    this.mark2.setVisibility(View.GONE);
                }
            } else {
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.GONE);
                this.desc.setText(nativeAd.getSummary());
                this.desc.setVisibility(View.VISIBLE);
                this.desc1.setVisibility(View.GONE);
                this.desc2.setVisibility(View.GONE);
                this.mark.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark2.setVisibility(View.GONE);

            }
        } else if (TextUtils.equals(nativeAd.getStyle(), AdConfig.AD_VIEW_TYPE_LIST_NATIVE_BIG)) {
            if (nativeAd.getThumbList() != null && nativeAd.getThumbList().length > 0) {
                LoadImageUtils.getInstance().loadImage(this.bigImg, nativeAd.getThumbList()[0], autoHeightControllerListener);
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.VISIBLE);
                this.desc2.setText(nativeAd.getSummary());
                this.desc2.setVisibility(View.VISIBLE);
                this.desc1.setVisibility(View.GONE);
                this.desc.setVisibility(View.GONE);
                this.mark2.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark.setVisibility(View.GONE);
            } else {
                this.oneImg.setVisibility(View.GONE);
                this.twoImg.setVisibility(View.GONE);
                this.threeImg.setVisibility(View.GONE);
                this.smallImg.setVisibility(View.GONE);
                this.bigImg.setVisibility(View.GONE);
                this.desc.setText(nativeAd.getSummary());
                this.desc.setVisibility(View.VISIBLE);
                this.desc1.setVisibility(View.GONE);
                this.desc2.setVisibility(View.GONE);
                this.mark.setVisibility(View.VISIBLE);
                this.mark1.setVisibility(View.GONE);
                this.mark2.setVisibility(View.GONE);

            }
        }

        this.nativeAd = nativeAd;
        this.setTag("suces");//设置标识表示加载广告成功
    }

    @Override
    public void onClick(View v) {
        if (mapEvent != null) {
            MobclickAgentUtils.event(UmEventId.game_click, mapEvent);
        }
        Intent intent = new Intent(getContext(), WebActivity.class);
        intent.putExtra("url", nativeAd.getJumpUrl());
        getContext().startActivity(intent);

        String serialId = getTag().toString();
        if (!TextUtils.isEmpty(serialId)) {
            AdConfig.staticsAd(serialId, AdConfig.TYPE_1, AdConfig.AD_TYPE_H5);
        }
    }
}
